package hust.game.stages;

import hust.game.asset.Assets;
import hust.game.asset.SpriteAccessor;
import hust.game.main.MainGame;
import hust.game.screen.GameScreen;

import java.text.DecimalFormat;

import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Back;
import aurelienribon.tweenengine.equations.Quart;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GLCommon;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.ui.Image;


public class SplashStage extends UIStages {
	private static final int PX_PER_METER = 400;

	private final OrthographicCamera camera = new OrthographicCamera();
	private final SpriteBatch batch = new SpriteBatch();
	private final TweenManager tweenManager = new TweenManager();
	private final Sprite name;
	private final Sprite st;
	private final Sprite strip;
	private final Sprite veil;
	
	public boolean isShowLoadingBar = false;
	
	Image loadingTrack;
	Image loadingProgress;
	float percent = 0;
	
	TextureRegion track;
	TextureRegion progress;
	TextureAtlas atlas;
	
	public SplashStage(TweenCallback callback) {
		// TODO Auto-generated constructor stub
		atlas = Assets.inst().get("data/splash/splash.pack", TextureAtlas.class);
		
		name = atlas.createSprite("name");
		name.setScale(0.3f);
		
		st = atlas.createSprite("st");
		st.setScale(0.3f);
		
		strip = atlas.createSprite("white");
		veil = atlas.createSprite("white");

		float wpw = 1f;
		float wph = wpw * Gdx.graphics.getHeight() / Gdx.graphics.getWidth();

		camera.viewportWidth = wpw;
		camera.viewportHeight = wph;
		camera.update();

		Sprite[] sprites = new Sprite[] { name, st};
		for (Sprite sp : sprites) {
			sp.setSize(sp.getWidth()/PX_PER_METER, sp.getHeight()/PX_PER_METER);
			sp.setOrigin(sp.getWidth()/2, sp.getHeight()/2);
		}

		name.setPosition(-0.520f, -0.056f);
		st.setPosition(-0.38f, -0.056f);

		strip.setSize(wpw, wph);
		strip.setOrigin(wpw/2, wph/2);
		strip.setPosition(-wpw/2, -wph/2);

		veil.setSize(wpw, wph);
		veil.setPosition(-wpw/2, -wph/2);
		veil.setColor(1, 1, 1, 0);

		Timeline.createSequence()
			.push(Tween.set(name, SpriteAccessor.POS_XY).targetRelative(-1, 0))
			.push(Tween.set(st, SpriteAccessor.POS_XY).targetRelative(1, 0))
			.push(Tween.set(strip, SpriteAccessor.SCALE_XY).target(1, 0))

			.pushPause(2.0f)
			.push(Tween.to(strip, SpriteAccessor.SCALE_XY, 0.8f).target(1, 0.6f).ease(Back.OUT))
			.push(Tween.to(name, SpriteAccessor.POS_XY, 0.8f).targetRelative(1, 0).ease(Quart.OUT))
			.push(Tween.to(st, SpriteAccessor.POS_XY, 0.8f).targetRelative(-1, 0).ease(Quart.OUT))
			.pushPause(1.5f)
			.push(Tween.to(strip, SpriteAccessor.SCALE_XY, 0.8f).target(1, 1).ease(Back.IN))
			.pushPause(1.0f)
//			.beginParallel()
//				.push(Tween.to(name, SpriteAccessor.POS_XY, 0.5f).targetRelative(1, 0).ease(Back.IN))
//				.push(Tween.to(st, SpriteAccessor.POS_XY, 0.5f).targetRelative(1, 0).ease(Back.IN))
//			.end()
//			.pushPause(2.0f)

			.setCallback(callback)
			.start(tweenManager);
	}
	
	public void setLoadingBar(){
		atlas = Assets.inst().get("data/splash/loading.pack", TextureAtlas.class);
		track = atlas.findRegion("track");
		progress = atlas.findRegion("progress");
		loadingTrack = new Image(track);
        loadingTrack.setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
        
        loadingProgress = new Image(progress);
        loadingProgress.setPosition(loadingTrack.getX() + 5, loadingTrack.getY() + 2.5f);
        
		this.addActor(loadingTrack);
		this.addActor(loadingProgress);
		
	}
	
	@Override
	public void act(float delta) {
		// TODO Auto-generated method stub
		super.act(delta);
		render();
	}
	
	public void dispose() {
		tweenManager.killAll();
		atlas.dispose();
		batch.dispose();
	}

	DecimalFormat df = new DecimalFormat("#.##");
	public void render() {
		tweenManager.update(Gdx.graphics.getDeltaTime()/3);
		if(loadingProgress != null){
        	// Interpolate the percentage to make it more smooth
			if(df.format(percent).equals("0,99")){
				MainGame.getInstance().setScreen(new GameScreen());
			}else{
				percent = Interpolation.linear.apply(percent, 1.5f, 0.0015f);
		        loadingProgress.setWidth(progress.getRegionWidth() * percent);
		        loadingProgress.invalidate();
			}
	    		
           
        }
       
		GLCommon gl = Gdx.gl;
		gl.glClearColor(0, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		strip.draw(batch);
		name.draw(batch);
		st.draw(batch);
		if (veil.getColor().a > 0.1f) veil.draw(batch);
		batch.end();
	}
}
